package com.tengju.bff.interfaces.manage.member.resource.facade;

import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.manage.member.resource.assembler.ResourceAssembler;
import com.tengju.bff.interfaces.manage.member.resource.vo.AddResourceForm;
import com.tengju.bff.interfaces.manage.member.resource.vo.DelResourceForm;
import com.tengju.bff.interfaces.manage.member.resource.vo.EditResourceForm;
import com.tengju.bff.interfaces.manage.member.resource.vo.GetResourceForm;
import com.tengju.bff.interfaces.manage.member.resource.vo.PageResourceVO;
import com.tengju.support.domain.model.resource.Position;
import com.tengju.support.domain.model.resource.Resource;
import com.tengju.support.domain.model.resource.ResourceId;
import com.tengju.support.domain.model.resource.ResourceRepository;
import com.tengju.user.domain.model.user.Staff;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


@Service
public class ResourceFacade {

    @Autowired
    private ResourceRepository resourceRepository;

    public Boolean addResource(AddResourceForm form, Staff staff) {
        Resource resource = ResourceAssembler.voToModel(form, staff);
        resourceRepository.add(resource);
        return true;
    }

    public Boolean editResource(EditResourceForm form, Staff staff) {
        Resource resource = resourceRepository.find(new ResourceId(form.getResourceId()));
        resource.edit(form.getTitle(),form.getPosition(),form.getImgUrl(),
                form.getJumpUrl(),form.getBeginTime(),form.getEndTime(),
                form.getUserOrientation(),form.getPriority(),staff.getName());
        resourceRepository.edit(resource);
        return true;
    }

    public Boolean delResource(DelResourceForm form) {
        resourceRepository.del(new ResourceId(form.getResourceId()));
        return true;
    }

    public PageResourceVO getResourceByPage(GetResourceForm form) {

        List<Position> positions = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(form.getPositions())) {
            positions = form.getPositions().stream().map(Position::fromVal).collect(Collectors.toList());
        }
        ResourceId resourceId = Optional.ofNullable(form.getResourceId())
                .map(ResourceId::new)
                .orElse(null);
        int total = resourceRepository.count(resourceId, form.getTitle(), positions);

        Integer pageSize = form.getPageSize();
        Integer pageIndex = form.getPageIndex();
        Integer offset = (pageIndex - 1) * pageSize;

        List<Resource> resources = Lists.newArrayList();
        if (offset <= total) {
            resources = resourceRepository.queryByPage(resourceId, form.getTitle(), positions, offset, pageSize);
        }

        return ResourceAssembler.modelToVO(resources,form,total);
    }
}
